/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-05-12 10:57:53
 * @LastEditors: voanit
 * @LastEditTime: 2022-05-12 16:31:34
 */
// {
//     "filter": "all",  // all（全部）, active(没有完成的) completed(完成的)
const initState = [
  {
    id: 1,
    name: '读书',
    isDone: true,
  },
  {
    id: 2,
    name: '写字',
    isDone: false,
  },
]
//   }
// action: { type: 'filter/mod', payload: 'active'}
export default function todos(state = initState, { payload, type }) {
  console.log(type, payload)
  switch (type) {
    case 'todos/add':
      return [...state, { id: state.length + 1, name: payload, isDone: true }]
    case 'todos/del':
      return state.filter((item) => item.id !== payload)
    case 'todos/all':
      return state.map((item) => {
        return {
          ...item,
          isDone: payload,
        }
      })
    // case 'todos/filterDone':
    //   return state.filter((item) => item.isDone)
    case 'todos/isDone':
      return state.map((item) => {
        if (item.id === payload.id) {
          return {
            ...item,
            isDone: payload.isDone,
          }
        } else {
          return item
        }
      })
    case 'todos/edit':
      return state.map((item) => {
        if (item.id === payload.id) {
          return {
            ...item,
            name: payload.name,
          }
        } else {
          return item
        }
      })
    // case 'todos/SelectActive':
    //   return state.filter((item) => !item.isDone)
    // case 'todos/SelectCompleted':
    //   return state.filter((item) => item.isDone)
    default:
      return state
  }
}
